package com.qf.service.impl;

import com.qf.entity.Leave;
import com.qf.entity.LeaveType;
import com.qf.entity.Task;
import com.qf.enums.OAExceptionEnum;
import com.qf.exception.OAException;
import com.qf.mapper.LeaveMapper;
import com.qf.mapper.LeaveTypeMapper;
import com.qf.mapper.TaskMapper;
import com.qf.service.LeaveTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author 程云
 * @Date 2019/12/10 11:10
 * 人生在世 代码二字
 */
@Service
@Slf4j
public class LeaveTypeServiceImpl implements LeaveTypeService {

    @Resource
    private LeaveTypeMapper leaveTypeMapper;

    @Resource
    private LeaveMapper leaveMapper;

    @Resource
    private TaskMapper taskMapper;

    //查询请假类型
    @Override
    public List<LeaveType> findAllLeaveType() {
        return leaveTypeMapper.selectAll();
    }

    //添加请假信息
    @Override
    @Transactional
    public Integer addleave(Leave leave) {
        //默认审核状态，0未审核
        leave.setAuditStatus(0);
        leave.setAuditTime(new Date());
        //请假
        leave.setTaskId(1);
        Integer count = leaveMapper.insertSelectives(leave);
        if (count != 1){
            log.info("【请假信息】 添加请假信息失败 count = {}",count);
            throw new OAException(OAExceptionEnum.LEAVE_ADD_ERROR.getCode(),OAExceptionEnum.LEAVE_ADD_ERROR.getMsg());
        }
        Task param = new Task();
        param.setApplyName(leave.getRealName());
        param.setApplyTime(leave.getAuditTime());
        param.setTaskId(leave.getTaskId());
        param.setStatus(leave.getAuditStatus());
        param.setUserId(leave.getUserId());
        param.setEventId(leave.getPersId());
        int num = taskMapper.insert(param);
        return count;
    }
}
